package com.aiti.lulian.controller.newCard;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.newCard.CardBaseInfoDto;
import com.aiti.lulian.dto.newCard.CardInfoDto;
import com.aiti.lulian.service.newCard.CardBusinessService;
import com.aiti.lulian.service.newCard.CardPersonalBaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description:
 * auth fcb
 * date 2025-08-12 14:25
 **/
@RestController
@RequestMapping("/new-card")
@Slf4j
@Api(tags = "名片业务接口")
public class CardBusinessController {

    @Autowired
    private CardBusinessService cardBusinessService;
    @Autowired
    private CardPersonalBaseInfoService cardPersonalBaseInfoService;

    @GetMapping("/get-card-detail")
    @ApiOperation("获取名片详情")
    public Message getCardDetail(@RequestParam(value = "cardId") String cardId,
                                 @RequestParam(value = "userId", required = false) String userId) {
        try {
            return Message.success(cardBusinessService.getCardDetail(cardId, userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取名片详情失败：{}", ExceptionUtil.stacktraceToString( e));
            return Message.fail();
        }
    }

    @PostMapping("/update-card-info")
    @ApiOperation("更新名片信息")
    public Message updateCardInfo(@RequestBody CardInfoDto cardInfoDto) {
        try {
            cardBusinessService.updateCardInfo(cardInfoDto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新名片信息失败：{}", ExceptionUtil.stacktraceToString( e));
            return Message.fail("操作失败");
        }
    }

    @DeleteMapping("/remove-card-info")
    @ApiOperation("删除名片信息")
    public Message removeCardInfo(@RequestParam(value = "cardId") String cardId) {
        try {
            cardBusinessService.removeCardInfo(cardId);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("删除名片信息失败：{}", ExceptionUtil.stacktraceToString( e));
            return Message.fail("操作失败");
        }
    }

    @GetMapping("/get-card-use-resource-type")
    @ApiOperation("获取名片使用资源类型")
    public Message getCardUseResourceType(@RequestParam(value = "cardId") String cardId) {
        return Message.success(cardBusinessService.getCardUseResourceType(cardId));
    }


    @GetMapping("/userCards")
    @ApiOperation("获取用户名片列表")
    public Message userCards(@RequestParam(value = "userId") String userId, @RequestParam(value = "backend", required = false) String backend) {
        return cardBusinessService.userCards(userId, backend);
    }

    @GetMapping("/queryEnterpriseCardList")
    @ApiOperation("获取用户企业名片列表")
    public Message queryEnterpriseCardList(@RequestParam(value = "userId") String userId) {
        return cardBusinessService.queryEnterpriseCardList(userId);
    }

    @PostMapping("/create-card-info")
    @ApiOperation("创建名片信息")
    public Message createCardInfo(@RequestBody CardBaseInfoDto cardBaseInfoDto) {
        try {
            return Message.success(cardBusinessService.createCardBaseInfo(cardBaseInfoDto));
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("创建名片信息失败：{}", ExceptionUtil.stacktraceToString( e));
            return Message.fail("操作失败");
        }
    }

    @GetMapping("/get-card-base-info")
    @ApiOperation("获取名片基本信息")
    public Message getCardBaseInfo(@RequestParam(value = "cardId", required = false) String cardId,
                                   @RequestParam("userId") String userId) {
        try {
            return Message.success(cardBusinessService.getCardBaseInfo(cardId, userId));
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取名片基本信息失败：{}", ExceptionUtil.stacktraceToString( e));
            return Message.fail();
        }
    }

    @PutMapping("update-pic-bg")
    @ApiOperation("更新名片图背景")
    public Message updatePicBg(@RequestBody CardBaseInfoDto dto) {
        cardBusinessService.updatePicBg(dto);
        return Message.success("设置成功");
    }

    @GetMapping("update-logged-in")
    @ApiOperation("更新名片登录状态")
    public Message updateLoggedIn(@RequestParam("cardId") String cardId) {
        cardBusinessService.updateLoggedIn(cardId);
        return Message.success();
    }

    @GetMapping("update-current-use")
    @ApiOperation("标记用户最新登录的名片")
    public Message updateCurrentUse(@RequestParam("cardId") String cardId) {
        cardBusinessService.updateCurrentUse(cardId);
        return Message.success();
    }

    @GetMapping("/increase-share-count")
    @ApiOperation("增加转发计数")
    public void increaseShareCount(@RequestParam("cardId") String cardId) {
        cardBusinessService.increaseShareCount(cardId);
    }

    /**
     * 今日访问量 我的访客 我收藏
     */
    @GetMapping("/get-data-statistics")
    @ApiOperation("获取统计数据")
    public Message getDataStatistics(@RequestParam("cardId") String cardId) {
        return Message.success(cardBusinessService.getDataStatistics(cardId));
    }

    @GetMapping("/user-current-use-info")
    @ApiOperation("获取用户当前使用名片信息")
    public Message getUserCurrentUseInfo(@RequestParam("username") String username) {
        try {
            return Message.success(cardPersonalBaseInfoService.getUserCurrentUseCardInfo(username));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取用户当前使用名片信息失败：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

    @GetMapping("/my-card")
    @ApiOperation("查询名片是不是属于某个用户")
    public Message queryUserCard(@RequestParam("userId") String userId, @RequestParam("cardId") String cardId) {
        try {
            return Message.success(cardPersonalBaseInfoService.queryUserCardCount(userId, cardId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询名片是不是属于某个用户异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @GetMapping("/card-is-enterprise")
    @ApiOperation("判断名片是不是企业版")
    public Message cardIsEnterprise(@RequestParam(value = "cardId") String cardId) {
        return Message.success(cardPersonalBaseInfoService.isEnterpriseCard(cardId));
    }

    @GetMapping("/get-personal-card-detail")
    @ApiOperation("获取个人版名片详情")
    public Message getPersonalCardDetail(@RequestParam(value = "cardId") String cardId,
                                         @RequestParam(value = "userId", required = false) String userId) {
        try {
            return cardBusinessService.getPersonalCardDetail(cardId, userId);
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取个人版名片详情失败：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail();
        }
    }

    /**
     * 目前仅使用了下面那个接口（getEnterpriseCardUsingData）
     */
    @GetMapping("/get-enterprise-using-data")
    @ApiOperation("获取企业使用数据")
    public Message getEnterpriseUsingData(@RequestParam(value = "enterpriseId") String enterpriseId) {
        return Message.success(cardBusinessService.getEnterpriseUsingData(enterpriseId));
    }

    @GetMapping("/get-enterprise-card-using-data")
    @ApiOperation("获取企业名片使用数据")
    public Message getEnterpriseCardUsingData(@RequestParam(value = "enterpriseId") String enterpriseId,
                                              @RequestParam(value = "templateId") String templateId) {
        return Message.success(cardBusinessService.getEnterpriseCardUsingData(enterpriseId, templateId));
    }

    /**
     * 目前仅使用了下面那个接口（getPersonalCardUsingData）
     */
    @GetMapping("/get-personal-using-data")
    @ApiOperation("获取个人使用数据")
    public Message getPersonalUsingData(@RequestParam(value = "cardId") String cardId) {
        return Message.success(cardBusinessService.getPersonalUsingData(cardId));
    }

//    @GetMapping("/get-personal-card-using-data")
//    @ApiOperation("获取个人名片使用数据")
//    public Message getPersonalCardUsingData(@RequestParam(value = "templateId") String templateId, @RequestParam("userId") String userId) {
//        return Message.success(cardBusinessService.getPersonalCardUsingData(templateId, userId));
//    }

    @GetMapping("/get-personal-card-using-data")
    @ApiOperation("获取个人名片使用数据")
    public Message getPersonalCardUsingData(@RequestParam(value = "cardId") String cardId) {
        return Message.success(cardBusinessService.getPersonalCardUsingData(cardId));
    }

    @GetMapping("/get-admin-info")
    @ApiOperation("获取企业的管理员信息")
    public Message getAdminInfoByEnterpriseId(@RequestParam(value = "enterpriseId") String enterpriseId) {
        return Message.success(cardBusinessService.getAdminInfoByEnterpriseId(enterpriseId));
    }

    @GetMapping("/card-is-auditing")
    @ApiOperation("判断名片是不是审核中")
    public Message cardIsAuditing(@RequestParam(value = "cardId") String cardId) {
        return Message.success(cardBusinessService.cardIsAuditing(cardId));
    }
}
